#include "ConnWidgte.h"

ConnWidgte::ConnWidgte(QWidget *parent)
    : QWidget(parent)
{
    InitWidte();
}

ConnWidgte::~ConnWidgte()
{
}
void ConnWidgte::InitWidte()
{
    QGroupBox* pGrpboxLocal = new QGroupBox(tr("Conn"));
    QLabel *clientLabel = new QLabel(pGrpboxLocal);
    clientLabel->setText(tr("&ClientID:"));
    QLabel *userLabel = new QLabel(pGrpboxLocal);
    userLabel->setText(tr("&User:"));
    QLabel *pwdLabel = new QLabel(pGrpboxLocal);
    pwdLabel->setText(tr("&Pwd:"));
    m_clientid = new QLineEdit(pGrpboxLocal);
    m_clientid->setText(tr("ClientID"));
    m_user = new QLineEdit(pGrpboxLocal);
    m_user->setText(tr("mqtt"));
    m_pwd = new QLineEdit(pGrpboxLocal);
    m_pwd->setText(tr("mqtt"));
    clientLabel->setBuddy(m_clientid);
    userLabel->setBuddy(m_user);
    pwdLabel->setBuddy(m_pwd);
    pConn = new QPushButton(tr("Conn"));
    pConn->setDefault(true);
    pStop = new QPushButton(tr("Disconn"));
    pStop->setDefault(true);
    pStop->setEnabled(false);
    QHBoxLayout *phlocalnetcfg = new QHBoxLayout;
    phlocalnetcfg->addWidget(clientLabel);
    phlocalnetcfg->addWidget(m_clientid);
    phlocalnetcfg->addWidget(userLabel);
    phlocalnetcfg->addWidget(m_user);
    phlocalnetcfg->addWidget(pwdLabel);
    phlocalnetcfg->addWidget(m_pwd);
    QHBoxLayout *pHlayout = new QHBoxLayout;
    pHlayout->addWidget(pConn);
    pHlayout->addWidget(pStop);
    QHBoxLayout *pVlayout = new QHBoxLayout;
    pVlayout->addLayout(phlocalnetcfg);
    pVlayout->addLayout(pHlayout);
    pGrpboxLocal->setLayout(pVlayout);
    QVBoxLayout *playout = new QVBoxLayout;
    playout->addWidget(pGrpboxLocal);
    setLayout(playout);
    connect(pConn, SIGNAL(clicked()), this, SLOT(SendConn()));
    connect(pStop, SIGNAL(clicked()), this, SLOT(SendDisconn()));
}
void ConnWidgte::SetMq(MQ* mq)
{
    m_mq = mq;
}
void ConnWidgte::SendConn()
{
    Payload Client_Identifier;
    memcpy((char*)&Client_Identifier.payload, m_clientid->text().toStdString().c_str(), m_clientid->text().length());
    short clientidlen = m_clientid->text().length();
    Client_Identifier.len[0] = HIGHT(clientidlen);
    Client_Identifier.len[1] = LOW(clientidlen);
    Payload user;
    memcpy((char*)&user.payload, m_user->text().toStdString().c_str(), m_user->text().length());
    short useridlen = m_user->text().length();
    user.len[0] = HIGHT(useridlen);
    user.len[1] = LOW(useridlen);
    Payload pwd;
    memcpy((char*)&pwd.payload, m_user->text().toStdString().c_str(), m_user->text().length());
    short pwdidlen = m_user->text().length();
    pwd.len[0] = HIGHT(pwdidlen);
    pwd.len[1] = LOW(pwdidlen);
    m_mq->Connect(&Client_Identifier, TRUE, 60, NULL, NULL, &user, &pwd);
    pConn->setEnabled(false);
    pStop->setEnabled(true);
}
void ConnWidgte::SendDisconn()
{
    m_mq->DisConnect();
    pStop->setEnabled(false);
    pConn->setEnabled(true);
}